package cn.tedu.egweibo2.controller;

import cn.tedu.egweibo2.base.response.JsonResult;
import cn.tedu.egweibo2.base.response.Status;
import cn.tedu.egweibo2.mapper.CommentMapper;
import cn.tedu.egweibo2.pojo.dto.CommentAddParam;
import cn.tedu.egweibo2.pojo.entity.Comment;
import cn.tedu.egweibo2.pojo.vo.CommentVO;
import cn.tedu.egweibo2.pojo.vo.UserVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/v1/comment/")
@Api(tags="03.评论管理模块")
public class CommentController {
    @Autowired
    CommentMapper commentMapper;
    @ApiOperation(value="发布微博")
    @PostMapping("insert")
    public JsonResult insertComment(@RequestBody CommentAddParam commentAddParam, @ApiIgnore HttpSession session){
        UserVO userVO= (UserVO)session.getAttribute("user");
        if(userVO==null){
            return new JsonResult(Status.NOT_LOGIN);
        }
        Comment comment=new Comment();
        BeanUtils.copyProperties(commentAddParam,comment);
        comment.setUserId(userVO.getId());
        comment.setCreateTime(new Date());
        commentMapper.insertComment(comment);
        return JsonResult.ok();
    }
    @GetMapping("selectByWeiboId")
    @ApiOperation(value="微博评论列表")
    public JsonResult selectByWeiboId(Long weiboId,@ApiIgnore HttpSession session){
        UserVO userVO= (UserVO)session.getAttribute("user");
        if(userVO==null){
            return new JsonResult(Status.NOT_LOGIN);
        }
        List<CommentVO>  list = commentMapper.selectByWeiboId(weiboId);
        return JsonResult.ok(list);
    }




}
